package com.apobates.forum.toro.proxy;

import com.apobates.forum.toro.BatchQueryFace;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * 多对多查询表达式
 * @param <R> 查询结果类型
 *
 * @author xiaofanku@live.cn
 * @since 20220517
 */
public class MapperMultiAttrExpression<R> {
    private final String targetAttr;
    private final String collectionType;
    private final String selectAttr;
    private final String selectAttrTypeName;
    private final String joinAttr;
    private final String joinAttrTypeName;
    private final BatchQueryFace<R> queryFace;
    /**
     * MapperMultiAttrExpression构造函数
     * @param targetAttr 目标对象的属性名称, 例: Vo.product
     * @param collectionType 目标属性集合的实例供应函数
     * @param selectAttr 查询参数的属性名称, 例: Vo.productId
     * @param selectAttrTypeName 查询参数的类型名称, 例: 基础类型: long, 类类型: java.lang.Long
     * @param joinAttr 连接的属性名称, 例: selectObj.id, 此时Vo.productId == selectObj.id
     * @param joinAttrTypeName 连接属性的类型名称. 例: 基础类型: long, 类类型: java.lang.Long
     * @param queryFace  批量查询接口
     */
    public MapperMultiAttrExpression(
            String targetAttr,
            String collectionType,
            String selectAttr,
            String selectAttrTypeName,
            String joinAttr,
            String joinAttrTypeName,
            BatchQueryFace<R> queryFace) {
        this.targetAttr = targetAttr;
        this.collectionType = collectionType;
        this.selectAttr = selectAttr;
        this.selectAttrTypeName = Optional.ofNullable(selectAttrTypeName).orElse(joinAttrTypeName);
        this.joinAttr = joinAttr;
        this.joinAttrTypeName = Optional.ofNullable(joinAttrTypeName).orElse(selectAttrTypeName);
        this.queryFace = queryFace;
    }

    public String getTargetAttr() {
        return targetAttr;
    }

    public String getSelectAttr() {
        return selectAttr;
    }

    public String getSelectAttrTypeName() {
        return selectAttrTypeName;
    }

    public String getJoinAttr() {
        return joinAttr;
    }

    public String getJoinAttrTypeName() {
        return joinAttrTypeName;
    }

    public BatchQueryFace<R> getQueryFace() {
        return queryFace;
    }

    public String getCollectionType() {
        return collectionType;
    }

    /**
     * 返回聚合属性语句构建器
     * @return
     */
    public MapperMultiAttrStatements.Argument buildRelationArgs(){
        return new MapperMultiAttrStatements()
                .new Argument(this.getTargetAttr())
                .collection(this.getCollectionType())
                .join(this.getJoinAttr())
                .joinType(this.getJoinAttrTypeName())
                .used(this.getSelectAttr())
                .usedType(this.getSelectAttrTypeName());
    }
}
